package com.jstyle.jclife.activity;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.AutomicHeart;
import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.MyWheelAdapter;
import com.jstyle.jclife.ble.BleManager;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.view.widget.views.WheelView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AutoHeartActivity extends BaseActivity {
    private static final String TAG = "AutoHeartActivity";
    @BindView(R.id.wheelView_autoHeart)
    WheelView wheelViewAutoHeart;
    List<String> list = new ArrayList<>();
    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    private AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_heart);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        sendData(SendCmdState.GET_AUTOMIC_HEART);
        for (int i = 0; i < 13; i++) {
            list.add(String.valueOf(i * 5));
        }
        btGoalBack.setText(getString(R.string.Automatic_health_monitoring));
        ScreenUtils.setTitleTypeface(btGoalBack);

        ivShare.setVisibility(View.GONE);
        MyWheelAdapter myWheelAdapter = new MyWheelAdapter(this, list, 1, 20, 12);
        wheelViewAutoHeart.setHasLaber(true);
        wheelViewAutoHeart.setLabel(getString(R.string.mins));
        myWheelAdapter.setCenterColor(getResources().getColor(R.color.wheelViewCenter_color));
        wheelViewAutoHeart.setViewAdapter(myWheelAdapter);

    }


    @OnClick({R.id.iv_back,R.id.bt_goal_confim})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_goal_confim:
                sendAutoHeart();

                break;
        }
    }

    private void sendAutoHeart() {
        if(!BleManager.getInstance().isConnected()){
            showToast(getString(R.string.disonnected));
            return;
        }
        int time= Integer.parseInt(list.get(wheelViewAutoHeart.getCurrentItem()));
        AutomicHeart automicHeart=new AutomicHeart();
        automicHeart.setOpen(2);
        automicHeart.setStartHour(0);
        automicHeart.setStartMinute(0);
        automicHeart.setEndHour(23);
        automicHeart.setEndMinute(59);
        automicHeart.setWeek(255);
        automicHeart.setTime(time);
        sendData(SendCmdState.SET_AUTOMIC_HEART,automicHeart);
        ScreenUtils.showSetSuccessFul(btGoalBack);

//        ScreenUtils.setDialogButtonTextColor(alertDialog);
    }

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState){
            case GET_AUTOMIC_HEART:
                String time=maps.get(DeviceKey.KWorkTime);
                int index=list.indexOf(time);
                wheelViewAutoHeart.setCurrentItem(index);
                Log.i(TAG, "dataCallback: "+time);
                break;
        }
    }
}
